package cn.web.web_module.controller;

import cn.itcast.pojo.role;
import cn.itcast.util.ResponseEntity;
import cn.message.service.IRoleService;
import cn.message.service.adminService;
import cn.web.web_module.entity.TbAdminRole;
import cn.web.web_module.entity.WebModule;
import cn.web.web_module.service.TbAdminRoleService;
import cn.web.web_module.service.WebModuleService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.web.bind.annotation.*;
import cn.message.service.adminService;

import javax.annotation.Resource;
import javax.xml.ws.Response;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * (WebModule)表控制层
 * 根据权限展示前端路由
 *
 * @author 小松
 * @since 2020-06-19 08:55:58
 */
@RestController
@RequestMapping("webModule")
public class WebModuleController {

    private static final String PREFIX = "/webModule/";
    /**
     * 服务对象
     */
    @Resource
    private WebModuleService webModuleService;

    @Resource
    private IRoleService iRoleService;

    @Resource
    private TbAdminRoleService tbAdminRoleService;





    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public WebModule selectOne(Integer id) {
        return this.webModuleService.queryById(id);
    }


    /**
     *
     * @Description: 增加前端显示左边导航栏菜单
     *
     * @auther: 张松
     * @date: 16:23 2020/7/28
     * @param: [webModule]
     * @return: cn.itcast.util.ResponseEntity
     *
     */
    @GetMapping("insertOne")
    public ResponseEntity insertOne(WebModule webModule){
        WebModule insert = webModuleService.insert(webModule);
        try{
            if (insert.getModuleId()>0)return new ResponseEntity("添加成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity("添加失败,请重新添加").error_300();

    }


    /**
     * @Description: 接收一个role信息, 返回相应权限的module
     * @auther: 小松
     * @date: 10:05 2020/6/19
     * @param: [role]
     * @return: java.lang.String
     */
    @GetMapping("getmodule")
    public ResponseEntity getModule(String role) {
        try {
            ArrayList<WebModule> module = webModuleService.getModule(role);
            return new ResponseEntity(module);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity().error_300();
    }

/*-------------------------------------------------------------------------------------------------------------------
  权限修改页面接口开始
  -------------------------------------------------------------------------------------------------------------------
 */
    /**
     *
     * @Description: 查找全部的角色对应的前端展示权限
     *
     * @auther: 张松
     * @date: 8:40 2020/7/29
     * @param: []
     * @return: cn.itcast.util.ResponseEntity
     *
     */
    @GetMapping("getAllRoleModule")
    public ResponseEntity getAllRoleModule(){
        Map<String, Object> allRoleModule = webModuleService.getAllRoleModule();
        try{
            if (!allRoleModule.isEmpty()||allRoleModule.size()>0){
                return  new ResponseEntity(allRoleModule);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity("获取数据失败,请重新获取").error_300();
    }

    /**
     *
     * @Description: 获取角色名称
     *
     * @auther: 张松
     * @date: 10:23 2020/7/29
     * @param: []
     * @return: cn.itcast.util.ResponseEntity
     *
     */
    @GetMapping("getAllRoleName")
    public ResponseEntity getAllRoleName(){
        Map<String, role> allRoleName = iRoleService.getAllRoleName();
        try{
            if (!allRoleName.isEmpty()||allRoleName.size()>0) return new ResponseEntity(allRoleName);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity("获取数据失败,请重新获取").error_300();
    }

    /**
     *
     * @Description: 删除角色
     *
     * @auther: 张松
     * @date: 11:17 2020/7/29
     * @param: [roleId]
     * @return: cn.itcast.util.ResponseEntity
     *
     */
    @PostMapping("deleteRole")
    public ResponseEntity deleteRole(Integer roleId){

        return webModuleService.deleteRole(roleId);
    }

    /**
     *
     * @Description: 修改管理员权限
     *
     * @auther: 张松
     * @date: 18:21 2020/7/29
     * @param: [roleId, moduleId]
     * @return: cn.itcast.util.ResponseEntity
     *
     */
    @PostMapping("updataRoleModule")
    public ResponseEntity updataRoleModule(@RequestBody Map<String,Object> map){
        Integer roleId = (Integer)map.get("roleId");
        ArrayList<Integer> moduleId = (ArrayList<Integer>)map.get("moduleId");
        int i = webModuleService.updataRoleModule(roleId, moduleId);
        if (i>0){
            return new ResponseEntity("修改成功");
        }
        return new ResponseEntity().error_300();
    }


    /**
     *
     * @Description: 查找全部的前端显示module
     *
     * @auther: 张松
     * @date: 15:24 2020/7/28
     * @param: []
     * @return: java.util.ArrayList<cn.web.web_module.entity.WebModule>
     *
     */
    @GetMapping("selectAllModule")
    public ResponseEntity selectAllModule(){
        List<WebModule> webModules = webModuleService.selectAllModule();
        try {
            if (!webModules.isEmpty()||webModules.size()!=0)return new ResponseEntity(webModules);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity("获取失败,请刷新页面").error_300();
    }


/*-------------------------------------------------------------------------------------------------------------------
  权限修改页面接口结束
  -------------------------------------------------------------------------------------------------------------------
 */


}